<template>
	<view class="announcement">
		<view class="content">
			<textarea class="text1" v-model="content" maxlength=200 placeholder='请输入' @input="sumfontnum"></textarea>
			<text class="text2">{{fontNum}}/200</text>
		</view>
		
		<view class="imgbox">
			<image v-for="(item,index) in imgs" class="imgitem" :src="item" style="width: 216rpx;height: 216rpx;" />
			<image class="imgitem" src="@/static/image/shangchuan.png" style="width: 216rpx;height: 216rpx;" @click="handleUploadzhaopian"/>
		</view>
		
		<view class="bottomContent">
			<view class="tijiao" @click="handleAddnotice">
				发布公告
			</view>
		</view>
	</view>
</template>

<script>
	import{addnotice}from "@/api/api.js"
	export default{
		data(){
			return{
				fontNum:0,
				imgs:[],
				content:'',
			}
		},
		onLoad() {
		},
		methods:{
			handleUploadzhaopian() {
				let that = this
				uni.chooseImage({
					count: 9, //默认9
					sizeType: ['compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album', 'camera'], //从相册选择
					success: (chooseImageRes) => {
						const tempFilePaths = chooseImageRes.tempFilePaths;
						console.log("tempFilePaths", tempFilePaths)
						for (let i = 0; i < tempFilePaths.length; i++) {
							uni.uploadFile({
								url: 'https://www.dayige.cc/api/index/upload', //仅为示例，非真实的接口地址
								filePath: tempFilePaths[i],
								name: 'file',
								// formData: {
								// 	'user': 'test'
								// },
								header: {
									"Content-Type": "application/form-data",
									"Authorization": 'Bearer' + ' ' + uni.getStorageSync("token")
								},
								success: (uploadFileRes) => {
									// console.log("图片路径//////", uploadFileRes);
									// console.log("图片路径//////", JSON.parse(uploadFileRes.data).data);
									if (that.imgs.length >= 9) {
										uni.showToast({
											title: '最多可上传9张图片！',
											duration: 1000,
											icon: "none",
										});
									} else {
										that.imgs.push(JSON.parse(uploadFileRes.data).data)
									}
								},
								fail(e) {
									console.log("上传图片", e)
								}
							});
						}
					}
				});
			
			
			},
			sumfontnum(e) {
				this.fontNum = e.detail.value.length
			},
			async handleAddnotice(){
				console.log("1111",this.content,this.imgs)
				// return
				let res = await addnotice({
					content:this.content,
					url:this.imgs.join(",")
				})
				if(res.code == 1000){
					uni.showToast({
						title: '公告发布成功！',
						duration: 1000,
						icon: "none",
					});
					setTimeout(()=>{
						uni.redirectTo({
							url:"./announcementsList"
						})
					},1500)
					
				}else{
					uni.showToast({
						title:res.message,
						duration: 1000,
						icon: "none",
					});
				}
			},
		}
	}
</script>

<style scoped lang="scss">
	.announcement{
		.content{
			width: 100%;
			height: 480rpx;
			padding: 30rpx;
			box-sizing:border-box;
			border-bottom: #F2F2F2 1rpx solid;
			.text1{
				width: 100%;
				height: 390rpx;
				font-size: 48rpx;
				font-family: PingFang SC-Semibold, PingFang SC;
				font-weight: 600;
				color: #A7A7A7;
				line-height: 50rpx;
			}
			.text2{
				margin-left: 570rpx;
				font-size: 40rpx;
				font-family: PingFang SC-Semibold, PingFang SC;
				font-weight: 600;
				color: #A7A7A7;
				line-height: 50rpx;
			}
		}
		.imgbox{
			padding: 30rpx;
			box-sizing: border-box;
			display: flex;
			flex-wrap: wrap;
			.imgitem{
				margin-right: 20rpx;
			}
			.imgitem:nth-child(3n){
				margin-right: 0rpx;
			}
		}
		
		.bottomContent{
			width: 100%;
			position: fixed;
			bottom: 0;
			/* 兼容 iOS 设备 */
			padding-bottom: env(safe-area-inset-bottom);
			.tijiao{
				width: 686rpx;
				height: 132rpx;
				background: linear-gradient(180deg, #FE5C5C 0%, rgba(255,78,78,0.67) 100%);
				border-radius: 60rpx 60rpx 60rpx 60rpx;
				font-size: 72rpx;
				font-family: PingFang SC-Semibold, PingFang SC;
				font-weight: 600;
				color: #FFFFFF;
				line-height: 132rpx;
				text-align: center;
				margin: 0 auto;
			}
		}
	}
</style>